<?php
/**
 * stringHelper
 * @author    Soochow University
 * @version   1.0
 * @exclude   作  成  日：2011/04/07
 */
class stringHelper{
    /**
     * 字母小写化
     * @param   string  $str
     * @return  string  $str
     */
    public static function toLower($str){

        return strtoLower($str);

    }

    /**
     * 字母大写化
     * @param   string  $str
     * @return  string  $str
     */
    public static function toUpper($str){

        return strtoupper($str);

    }
    
    /**
     * 取得文字长度
     * @param   string  $str
     * @return  int
     */
    public static function strLength($str){

        return strlen($str);

    }
    
    /**
     * 左端文字列去除
     * @param   string  $str
     * @param   string  $charlist
     * @return  string  $str
     */
    public static function leftTrim($str,$charlist = null){

        return ltrim($str, $charlist);

    }
    
    /**
     * 右端文字列去除
     * @param   string  $str
     * @param   string  $charlist
     * @return  string  $str
     */
    public static function rightTrim($str,$charlist = null){

        return rtrim($str, $charlist);

    }
    
    /**
     * 指定文字列取得
     * @param   string  $str
     * @param   int     $start
     * @param   int     $count
     * @return  string  $str
     */
    public static function subString($str, $start, $count){

        return substr($str, $start, $count);
    }

    /**
     * ID检查
     * @param   string  $id
     * @return  boolen
     */
    public static function idCheck($id){
        return ereg("^[0-9a-zA-Z.-]+$", $id);
    }

    /**
     * 左侧补0
     * @param   string  $str
     * @param   int     $count
     * @return  string  $str
     */
    public static function zeroLeftAdd($str, $count=0){

        if (strlen($str) < $count)
            return str_repeat("0", $count-strlen($str)).$str;
        else 
            return $str;
    }

    /**
     * 删除所有空格
     * @param   string  $str
     * @return  string  $new_str
     */
    public static function totrim($str)
    {
       $new_str=null;
       $arr=str_split($str);
       foreach($arr as $value)
       {
           if($value!=" ")
           {
               $new_str.=$value;
           }
       }
       return $new_str;
    }

    /**
     * 指定长度文字切取
     * @param   string  $str
     * @param   int     $length
     * @return  string  $str
     */
    public static function cutString($str,$length)
    {
       $str=trim($str);
       if($length>0&&$length<=strlen($str)){
          $new_str=substr($str,0,$length)."...";
          return $new_str;
       }else{
          return $str;
       }
    }

    /**
     * 随机数生成
     * @param   integer $count
     * @param   string  $diff '0':数字/'1':英数字
     * @return  string  $str
     */
    public static function randString($count,$diff)
    {
        $arr=array("0","1","2","3","4","5","6","7","8","9",
            "a","b","c","d","e","f","g","h","i","j","k","l","m",
            "n","o","p","q","r","s","t","u","v","w","x","y","z",
            "A","B","C","D","E","F","G","H","I","J","K","L","M",
            "N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
        $str="";
        if(isset($count) && $count>=1){
          if($diff=='0'){
              for($i=0;$i<$count;$i++){
                  $str=$str.rand(0,9);
              }
          }
          else if($diff=='1'){
              for($i=0;$i<$count;$i++){
                  $str=$str.$arr[rand(0,count($arr) - 1)];
              }
          }
          return $str;
        }
    }

    /**
     * 左端0切除
     * @param   string  $str
     * @return  string  $str
     */
    public static function zeroShift($str){
        return ltrim($str, "0");
    }

    /**
     * 右端添0
     * @param   string  $str
     * @param   int     $zeroNumber
     * @return  string  $str
     */
    public static function zeroFormat($str, $zeroNumber=0){
        if (strlen($str) < $zeroNumber)
            return $str.str_repeat("0", $zeroNumber-strlen($str));
        else 
            return $str;
    }

    /**
     * 左右空格去除
     * @param   string  $str
     * @return  string  $str
     */
    public static function trim($str){
        if(is_null($str)|| is_array($str)){
            return "";
        }
        return ltrim(rtrim($str));
    }

    /**
     * 文字列长度取得
     * @param   string  $str
     * @return  int     $n
     */
    public static function getLength($str){
        $n=0;
        $p=0;
        $c='';
        $len=strlen($str);
        for($i=0;$i<$len;$i++){
            $c=ord($str{$i});
            if($c>252){
                $p=5;
            }elseif($c>248){
                $p=4;
            }elseif($c>240){
                $p=3;
            }elseif($c>224){
                $p=2;
            }elseif($c>192){
                $p=1;
            }else{
                $p=0;
            }
            $i+=$p;
            $n++;
        }
        return $n;
        
    }

    /**
     * 文字列2进制长度
     * @param   string  $str
     * @return  int
     */
    public static function getBLength($str){

        return strlen($str);

    }

    /**
     * html变化
     * @param   string  $str
     * @return  string  $str
     */
    public static function htmlSpecialCharDecode($str) {
        return htmlspecialchars_decode($str);
    }

    /**
     * "\r\n"变成"<br/>"
     * @param   string  $str
     * @return  string  $str
     */
    public static function insertHtmlLine($str){
        return nl2br($str);
    }
}
